package com.bootdo.wx.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.bootdo.common.utils.*;
import com.bootdo.common.utils.excel.OrderStatisticsExcelUtils2;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.dao.OrderDetailDao;
import com.bootdo.wx.dao.OrderShippingDao;
import com.bootdo.wx.domain.*;
import com.bootdo.wx.enums.OrderStatus;
import com.bootdo.wx.service.*;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 订单信息表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-05-27 17:57:00
 */
 
@Controller
@RequestMapping("/wx/orderOrder")
public class OrderOrderController {
	@Autowired
	private OrderOrderService orderOrderService;
	@Autowired
	private ShopExpressCodeService shopExpressCodeService;
	@Autowired
	private OrderShippingService orderShippingService;
	@Autowired
	private MemberPriceService memberPriceService;
	@Autowired
	private SetWechatService setWechatService;
	@Autowired
	private WeChatPayService weChatPayService;
	@Autowired
	private OrderComService orderComService;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private OrderDetailDao orderDetailDao;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	@Autowired
	private OrderBackService orderBackService;
	@Autowired
	private OrderShippingDao orderShippingDao;
	@Autowired
	private OrderBackAdressService orderBackAdressService;


	
	@GetMapping("/{flag}")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String OrderOrder(@PathVariable("flag") String flag,Model model){
		model.addAttribute("selectFlag", flag);
	    return "wx/orderOrder/orderOrder";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		if(params.get("status")!=null&&!"".equals(params.get("status"))){
			if(params.get("status").equals("2")){
				params.put("ordertype", "1");
				params.remove("status");
			}
		}
		Query query = new Query(params);
		List<Map<String,Object>> orderOrderList = orderOrderService.getOrderList(query);
		int total = orderOrderService.getOrderListCount(query);
		PageUtils pageUtils = new PageUtils(orderOrderList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:orderOrder:add")
	String add(){
	    return "wx/orderOrder/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:orderOrder:edit")
	String edit(@PathVariable("id") String id,Model model){
		OrderOrderDO orderOrder = orderOrderService.get(id);
		model.addAttribute("orderOrder", orderOrder);
	    return "wx/orderOrder/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:orderOrder:add")
	public R save( OrderOrderDO orderOrder){
		if(orderOrderService.save(orderOrder)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:orderOrder:edit")
	public R update( OrderOrderDO orderOrder){
		orderOrderService.update(orderOrder);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:orderOrder:remove")
	public R remove( String id){
		if(orderOrderService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:orderOrder:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		orderOrderService.batchRemove(ids);
		return R.ok();
	}

	/*
	* 去发货
	* */
	@GetMapping("/shipments/{id}")
	@RequiresPermissions("wx:orderOrder:edit")
	String shipments(@PathVariable("id") String id,Model model){
		OrderOrderDO orderOrder = orderOrderService.get(id);
		model.addAttribute("orderOrder", orderOrder);
		List<ShopExpressCodeDO> list = shopExpressCodeService.list(new HashMap<>());
		model.addAttribute("codeList", list);
		return "wx/orderOrder/shipments";
	}
/* public void send(Map<String, Object> params){
                String orderId = params.get("orderId").toString();
                OrderOrderDO orderOrderDO = orderOrderService.get(orderId);
                MemberUserDO memberUserDO = memberUserService.get(orderOrderDO.getUserId());
                StoreStoreDO storeStoreDO = storeStoreService.get(orderOrderDO.getStoreId());
                cn.hutool.json.JSONObject body=new cn.hutool.json.JSONObject();
                body.set("touser",memberUserDO.getWechatOpenId());
                if(params.get("type").toString().equals("0")){
                    //支付成功推送
                    body.set("template_id","LLkiNbkOpXjXkbSXFa81uSScSStlhODcu--M0bFhRXk");
                    cn.hutool.json.JSONObject json=new cn.hutool.json.JSONObject();
                    json.set("character_string6",new cn.hutool.json.JSONObject().set("value",orderOrderDO.getOrderSn()));
                    log.error("character_string6++++++++"+orderOrderDO.getOrderSn());
                    json.set("thing16",new cn.hutool.json.JSONObject().set("value", storeStoreDO.getName()));
                    log.error("thing16++++++++"+orderOrderDO.getOrderSn());
                    json.set("amount3",new cn.hutool.json.JSONObject().set("value",orderOrderDO.getPayment()+"元"));
                    log.error("amount3++++++++++++++++"+orderOrderDO.getOrderSn());
                    json.set("phrase8",new cn.hutool.json.JSONObject().set("value", "支付成功"));
                    log.error("phrase8+++++++++++++++++支付成功");
                    json.set("date5",new cn.hutool.json.JSONObject().set("value",DateUtils.format(orderOrderDO.getPayAt(), DateUtils.DATE_PATTERN_nian)));
                    log.error("date5+++++++++++++++++"+new cn.hutool.json.JSONObject().set("value",DateUtils.format(orderOrderDO.getPayAt(), DateUtils.DATE_PATTERN_nian)));
                    body.set("data",json);
                }else if(params.get("type").toString().equals("1")){
                    //到货推送
                    body.set("template_id","jme7rbn-ufZ3sMoOdMgm8UCEdibdECP7O3YBsCQGils");
                    cn.hutool.json.JSONObject json=new cn.hutool.json.JSONObject();
                    json.set("character_string1",new cn.hutool.json.JSONObject().set("value",orderOrderDO.getOrderSn()));
                    json.set("thing6",new cn.hutool.json.JSONObject().set("value", storeStoreDO.getName()));
                    json.set("phone_number10",new cn.hutool.json.JSONObject().set("value",storeStoreDO.getPhone()));
                    json.set("thing5",new cn.hutool.json.JSONObject().set("value", storeStoreDO.getAddress()));
                    body.set("data",json);
                }
                //发送
                String accessToken= getAccessToken();
                String post =  cn.hutool.http.HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken, body.toString());
                System.out.println(post);
            }*/
	/**
	 * 确认、修改发货
	 */
	@ResponseBody
	@PostMapping("/setShipments")
	@RequiresPermissions("wx:orderOrder:edit")
	public R setShipments( OrderOrderDO orderOrder) throws WxErrorException {
		int status = orderOrder.getStatus();
		if (status != 3 && status != 4) {
			return R.error("订单状态错误");
		}

		//添加物流
		if (status == 3) {
			if(orderOrderService.addShipping(orderOrder)>0){
				return R.ok();
			}
		}
		//修改物流
		if (status == 4) {
			if(orderOrderService.editShipping(orderOrder)>0){
				return R.ok();
			}
		}
		return R.error();
	}

	/*修改收货地址*/
	@GetMapping("/editShipping/{id}")
	@RequiresPermissions("wx:orderShipping:edit")
	String editShipping(@PathVariable("id") String id,Model model){
		OrderShippingDO orderShippingDO = orderShippingService.getByOrderId(id);
		model.addAttribute("orderShipping", orderShippingDO);
		return "wx/orderShipping/edit";
	}

	/*
	* 订单详情
	* */
	@GetMapping("/orderInfo/{id}")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String orderInfo(@PathVariable("id") String id,Model model){
		Map<String,Object> map = orderOrderService.getHOrderById(id);
		model.addAttribute("order", map);
		return "wx/orderOrder/orderInfo";
	}
	/*
	 * 返利详情页面
	 * */
	@GetMapping("/orderComList/{id}")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String orderComList(@PathVariable("id") String id,Model model){
		model.addAttribute("id", id);
		return "wx/orderOrder/orderComList";
	}


	
	/**
	 * 获取返利详情
	 */
	@ResponseBody
	@RequestMapping("/getOrderComList")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	public PageUtils getOrderComList(@RequestParam Map<String, Object> params){
		params.put("delFlag", "0");
		Query query = new Query(params);
		List<OrderComDO> list = orderComService.list(query);
		List<Map<String, Object>> returnList = new ArrayList<>();
		if(list.size()>0){
			for (OrderComDO orderComDO : list) {
				Map<String, Object> entityToMap = StringUtils.entityToMap(orderComDO);
				MemberUserDO memberUserDO = memberUserDao.get(orderComDO.getUserId());
				if(memberUserDO!=null){
					entityToMap.put("userName", memberUserDO.getName());
					entityToMap.put("userPhone", memberUserDO.getPhone());
					entityToMap.put("level", memberUserDO.getLevel());
					entityToMap.put("money", orderComDO.getComPrice());
					entityToMap.put("avator", memberUserDO.getAvator());
					returnList.add(entityToMap);
				}else{
					entityToMap.put("userName", "");
					entityToMap.put("userPhone", "");
					entityToMap.put("level", "");
					entityToMap.put("avator", "");
					entityToMap.put("money", orderComDO.getComPrice());
					returnList.add(entityToMap);
				}
			}
		}
		int total = orderComService.count(query);
		PageUtils pageUtils = new PageUtils(returnList, total);
		return pageUtils;
	}

	/*去统计页面*/
	@GetMapping("/statistics")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String statistics(@RequestParam Map<String, Object> params,Model model){
		model.addAttribute("map", params);
		return "wx/orderOrder/statisticsList";
	}

	/**
	 * 统计查询
	 */
	@ResponseBody
	@RequestMapping("/statisticsList")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	public PageUtils statisticsList(@RequestParam Map<String, Object> params){
		//String status = params.get("status").toString();
		//查询列表数据
		Query query = new Query(params);
		List<Map<String,Object>> orderOrderList = orderOrderService.statisticsList(query);
		int total = orderOrderService.statisticsCount(query);
		PageUtils pageUtils = new PageUtils(orderOrderList, total);
		return pageUtils;
	}

	/*去订单商品详情页面*/
	@GetMapping("/detailGoods/{id}")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String detailGoods(@PathVariable("id") String id,Model model){
		model.addAttribute("id",id);
		return "wx/orderOrder/detailGoods";
	}

	/**
	 * 获取订单商品详情
	 */
	@ResponseBody
	@RequestMapping("/getDetailGoods")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	public PageUtils getDetailGoods(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<Map<String,Object>> orderOrderList = orderOrderService.getDetailGoods(query);
		int total = orderOrderService.getDetailGoodsCount(query);
		PageUtils pageUtils = new PageUtils(orderOrderList, total);
		return pageUtils;
	}

	/*去砍价人员页面*/
	@GetMapping("/kUsers/{id}")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String kUsers(@PathVariable("id") String id,Model model){
		model.addAttribute("id",id);
		return "wx/orderOrder/kUsers";
	}

	/**
	 * 获取砍价人员详情
	 */
	@ResponseBody
	@RequestMapping("/getkUsers")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	public PageUtils getkUsers(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<Map<String,Object>> orderOrderList = orderOrderService.getKUsers(query);
		int total = orderOrderService.getKUsersCount(query);
		PageUtils pageUtils = new PageUtils(orderOrderList, total);
		return pageUtils;
	}

	/**
	 * 导出代发货excel表格
	 */
	@RequestMapping( "/exportExcel")
	@RequiresPermissions("wx:orderOrder:excel_out")
	public void exportExcel(@RequestParam("orderSn") String orderSn,@RequestParam("eTime") String eTime,
							@RequestParam("sTime") String sTime,@RequestParam("exports") String export,
							@RequestParam("selectFlag") String selectFlag,
							HttpServletResponse response,HttpServletRequest request){
		try
		{
			//1.查找代发货订单列表
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("orderSn",orderSn);
			map.put("eTime",eTime);
			map.put("sTime", sTime);
			map.put("export", export);
			map.put("status","3");
			map.put("selectFlag",selectFlag);
			List<Map<String,Object>> orderList = orderOrderService.getOrderList(map);
			for (Map<String,Object> orderMap : orderList) {
				List<Map<String,Object>> goodsList = orderOrderService.getDetailGoods(orderMap);
				orderMap.put("goodsList",goodsList);
				HashMap<String, Object> objectObjectHashMap = MapUtil.newHashMap();

				objectObjectHashMap.put("userId",MapUtil.getStr(orderMap,"userId"));
				objectObjectHashMap.put("level","0");
				objectObjectHashMap.put("groupby","1");
				List<MemberUserDO> myUpVip = memberUserDao.getMyUpVip(objectObjectHashMap);
				orderMap.put("upUserList",myUpVip);
			}
			//这里设置的文件格式是application/x-excel
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String("待发货订单列表.xls".getBytes(), "ISO-8859-1"));
			ServletOutputStream outputStream = response.getOutputStream();
			orderOrderService.exportExcel(map,orderList, outputStream,request);
			if(outputStream != null) {
				outputStream.close();
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 导出统计excel表格
	 */
	@RequestMapping( "/statisticsExportExcel")
	@RequiresPermissions("wx:orderOrder:excel_out")
	public void exportExcel(@RequestParam("orderSn") String orderSn,@RequestParam("eTime") String eTime,
			@RequestParam("sTime") String sTime,@RequestParam("exports") String export,@RequestParam("status") String status,@RequestParam("flag") String flag,
			HttpServletResponse response,HttpServletRequest request){
		try
		{
			//1.查找代发货订单列表
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("orderSn",orderSn);
			map.put("eTime",eTime);
			map.put("sTime", sTime);
			map.put("export", export);
			map.put("status",status);
			map.put("flag",flag);
			List<Map<String,Object>> orderOrderList = orderOrderService.statisticsList(map);
			//这里设置的文件格式是application/x-excel
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + new String("统计信息列表.xls".getBytes(), "ISO-8859-1"));
			ServletOutputStream outputStream = response.getOutputStream();
			OrderStatisticsExcelUtils2.exportUserExcel(orderOrderList,map, outputStream);
			if(outputStream != null) {
				outputStream.close();
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 导入代发货excel表格
	 */
	@ResponseBody
	@RequestMapping( "/importExcel")
	@RequiresPermissions("wx:orderOrder:excel_in")
	public R importExcel(@RequestParam("file")MultipartFile multipartFile, HttpServletResponse response){
		/*try {
			FileUtil.multipartFileToFile(multipartFile);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		if(multipartFile!= null) {
			String userExcelFileName = multipartFile.getOriginalFilename();
			//判断是否是Excel文件
			if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$"))
			{
				orderOrderService.importExcel(multipartFile, userExcelFileName);
			}
		}
		int a = 0;
		return R.ok();
	}

	@GetMapping("/back")
	@RequiresPermissions("wx:orderOrder:back")
	String back(){
		return "wx/orderOrder/backOrder";
	}

	@GetMapping("/getOrderBackImg/{id}")
	@ResponseBody
	@RequiresPermissions("wx:orderOrder:back")
	public R back(@PathVariable("id") String id,Model model){
		String[] split = id.split(",");
		//OrderOrderDO orderOrderDO = orderOrderService.get(split[0]);
		OrderBackDO orderBackDO = orderBackService.getByOrderId(split[0]);
		if(Integer.parseInt(split[1])==0){
			//退货图片凭证
			String img = orderBackDO.getImg();
			String[] imgList = StrUtil.split(img, ",");
			//model.addAttribute("imgList",imgList);
			HashMap<Object, Object> objectObjectHashMap = MapUtil.newHashMap();
			objectObjectHashMap.put("title","退货图片凭证");
			objectObjectHashMap.put("id", orderBackDO.getId());
			objectObjectHashMap.put("start",0);
			List<Map<Object, Object>> list = new ArrayList<>();
			if(imgList.length>0){
				for (int i = 0; i < imgList.length; i++) {
					HashMap<Object, Object> objectObjectHashMap1 = MapUtil.newHashMap();
					objectObjectHashMap1.put("alt","图片"+(i+1));
					objectObjectHashMap1.put("pid",IdUtil.randomUUID());
					objectObjectHashMap1.put("src",imgList[i]);
					objectObjectHashMap1.put("thumb","");
					list.add(objectObjectHashMap1);
				}
			}
			objectObjectHashMap.put("data",list);
			return R.ok().put("data",objectObjectHashMap);
		}else {
			//退款图片说明
			String img = orderBackDO.getImgurl();
			String[] imgList = StrUtil.split(img, ",");
			model.addAttribute("imgList",imgList);
			HashMap<Object, Object> objectObjectHashMap = MapUtil.newHashMap();
			objectObjectHashMap.put("title","退货图片凭证");
			objectObjectHashMap.put("id", orderBackDO.getId());
			objectObjectHashMap.put("start",0);

			List<Map<Object, Object>> list = new ArrayList<>();
			if(imgList.length>0){

				for (int i = 0; i < imgList.length; i++) {
					HashMap<Object, Object> objectObjectHashMap1 = MapUtil.newHashMap();
					objectObjectHashMap1.put("alt","图片"+(i+1));
					objectObjectHashMap1.put("pid",IdUtil.randomUUID());
					objectObjectHashMap1.put("src",imgList[i]);
					objectObjectHashMap1.put("thumb","");
					list.add(objectObjectHashMap1);
				}
			}
			objectObjectHashMap.put("data",list);
			return R.ok().put("data",objectObjectHashMap);
		}
		//model.addAttribute("id",split[0]);

//		{
//			"title": "", //相册标题
//				"id": 123, //相册id
//				"start": 0, //初始显示的图片序号，默认0
//				"data": [   //相册包含的图片，数组格式
//			{
//				"alt": "图片名",
//					"pid": 666, //图片id
//					"src": "", //原图地址
//					"thumb": "" //缩略图地址
//			}
// 					 ]
//		}

		//return "wx/orderOrder/getOrderBackImg";
	}

	/*
	* 退货订单列表
	* */
	@ResponseBody
	@GetMapping("/backList")
	@RequiresPermissions("wx:orderOrder:back")
	public PageUtils backList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<Map<String,Object>> orderOrderList = orderOrderService.getBackOrderList(query);
		int total = orderOrderService.getBackOrderListCount(query);
		PageUtils pageUtils = new PageUtils(orderOrderList, total);
		return pageUtils;
	}


	/*去填写退货物流页面*/
	@GetMapping("/goBackOrderAddress/{id}")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String goBackOrderAddress(@PathVariable("id") String id,Model model){
		model.addAttribute("orderId",id);
		model.addAttribute("status",1);
		return "wx/orderOrder/goBackOrderInfo";
	}
	@PostMapping( "/saveOrderBackAddress")
	@ResponseBody
	@RequiresPermissions("wx:orderOrder:backOrder")
	public R backOrder(OrderBackAdressDO orderBackAdressDO) throws Exception {
		orderBackAdressDO.setId(IdUtil.simpleUUID());
		orderBackAdressDO.setCaAt(new Date());
		orderBackAdressDO.setDelFlag(0);
		orderBackAdressService.save(orderBackAdressDO);
		OrderOrderDO orderOrderDO = new OrderOrderDO();
		orderOrderDO.setId(orderBackAdressDO.getOrderId());
		orderOrderDO.setStatus(1);
		return this.backOrder(orderOrderDO);
	}
	/**
	 * 后台退货订单操作
	 * 商家收到货,开始退款
	 */
	@PostMapping( "/backOrder")
	@ResponseBody
	@RequiresPermissions("wx:orderOrder:backOrder")
	public R backOrder(OrderOrderDO order) throws Exception {
		if (order.getStatus() == 6) {
			OrderOrderDO orderOrderDO = orderOrderService.get(order.getId());
			if (orderOrderDO.getPaytype() == 2) {  //余额支付
				//复原余额
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("gId",order.getId());
				map.put("type","0");
				MemberPriceDO memberPriceDO = memberPriceService.list(map).get(0);
				memberPriceDO.setDelFlag(1);
				memberPriceService.update(memberPriceDO);
				
			} else if (orderOrderDO.getPaytype() == 0) {  //微信支付
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("orderId",order.getId());
				//ApiWeChatPayController apiWeChatPayController = new ApiWeChatPayController();
				try {
					R refund = weChatPayService.refund(map);
					System.out.println(refund);
					return refund;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		if (orderOrderService.backOrder(order) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/*去自提订单页面*/
	@GetMapping("/postType")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String postType(@RequestParam Map<String, Object> params,Model model){
		return "wx/orderOrder/postOrder";
	}

	/**
	 * 自提订单列表
	 */
	@ResponseBody
	@RequestMapping("/postOrderList")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	public PageUtils postOrderList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<Map<String,Object>> orderOrderList = orderOrderService.getOrderList(query);
		int total = orderOrderService.getOrderListCount(query);
		PageUtils pageUtils = new PageUtils(orderOrderList, total);
		return pageUtils;
	}

	/**
	 * 自提操作
	 */
	@ResponseBody
	@PostMapping("/ziTi")
	@RequiresPermissions("wx:orderOrder:edit")
	public R ziTi( OrderOrderDO orderOrder){
		String id = orderOrder.getId();
		OrderOrderDO orderDO = orderOrderService.get(id);
		if (orderDO.getStatus() != 11) {
			return R.error("该订单状态错误 ：orderDO.getStatus()");
		}
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("userId",orderDO.getUserId());
		paramMap.put("type","7");
		paramMap.put("orderId",id);
		if(orderOrderService.setStatusOrder(paramMap) == 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 线下支付
	 */
	@PostMapping( "/price")
	@ResponseBody
	@RequiresPermissions("wx:orderOrder:edit")
	public R price( String id){
		OrderOrderDO orderDO = orderOrderService.get(id);
		Map<String,Object> priceMap = new HashMap<String,Object>();
		priceMap.put("userId",orderDO.getUserId());
		priceMap.put("orderId",orderDO.getId());
		priceMap.put("payType", "1");
		return orderOrderService.pricePay(priceMap);
	}


	@GetMapping("/modify")
	@RequiresPermissions("wx:order:modify")
	String modify(){
		return "wx/orderOrder/modifyOrder";
	}

	/*
	 * 换货订单列表 /wx/orderOrder/modify
	 * */
	@ResponseBody
	@GetMapping("/modifyList")
	@RequiresPermissions("wx:order:modify")
	public PageUtils modifyList(@RequestParam Map<String, Object> params){
		List<Map<String, Object>> list = orderOrderService.getModifyOrderList(params);
		int total = list.size();
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}

	/*后台换货通过，去修改物流*/
	@GetMapping("/modifyShipments/{id}")
	@RequiresPermissions("wx:order:editModify")
	String modifyShipments(@PathVariable("id") String id,Model model){
		List<ShopExpressCodeDO> list = shopExpressCodeService.list(new HashMap<>());
		model.addAttribute("codeList", list);
		model.addAttribute("id", id);  //订单id
		return "wx/orderOrder/modifyShipments";
	}

	/**
	 * 后台换货同意
	 */
	@PostMapping( "/modifyOrderT")
	@ResponseBody
	@RequiresPermissions("wx:order:editModify")
	public R modifyOrderT(@RequestParam Map<String,Object> params){
		return orderOrderService.modifyOrderT(params);
	}

	/**
	 * 后台换货驳回
	 */
	@PostMapping( "/modifyOrderF")
	@ResponseBody
	@RequiresPermissions("wx:order:editModify")
	public R modifyOrderF(OrderOrderDO order){
		return orderOrderService.modifyOrderF(order);
	}


	@GetMapping("/getOrderBackFormat/{id}")
	@RequiresPermissions("wx:orderOrder:orderOrder")
	String getOrderBackFormat(@PathVariable("id") String id,Model model){
		model.addAttribute("id", id);
		OrderOrderDO orderOrderDO = orderOrderService.get(id);
		MemberUserDO memberUserDO = memberUserDao.get(orderOrderDO.getUserId());
		List<Map<String, Object>> byOrderId = orderDetailDao.getByOrderId(id);
		GoodsGoodsDO goodsGoodsDO = goodsGoodsService.get(MapUtil.getStr(byOrderId.get(0), "goodId"));
		int total = MapUtil.getInt(byOrderId.get(0), "total");
		String shoppingCode = orderOrderDO.getShippingCode()!=null?orderOrderDO.getShippingCode():"无";
		OrderBackDO orderBackDO = orderBackService.getByOrderId(orderOrderDO.getId());
		OrderShippingDO orderShippingDO = orderShippingDao.getByOrderId(orderOrderDO.getId());
		String backExpressCode = "无";
		if(orderBackDO!=null){
			backExpressCode = orderBackDO.getExpressCode();
		}

		String backReason = "无";
		if(orderBackDO!=null&&StringUtils.isNotBlank(orderBackDO.getReason())){
			backReason = orderBackDO.getReason();
		}


		String content = "★反馈人："+memberUserDO.getName()+"\n" +
				"★订单号："+orderOrderDO.getOrderSn()+"\n" +
				"★商品名称："+goodsGoodsDO.getName()+"\n" +
				"★商品数量："+total+"\n" +
				"★原始单号："+shoppingCode+"\n" +
				"★寄回单号："+backExpressCode+"\n" +
				"★下单时间："+ DateUtil.format(orderOrderDO.getCreateAt(), DateUtils.DATE_TIME_PATTERN)+"\n" +
				"★订单信息："+orderShippingDO.getReceiverName()+" "+orderShippingDO.getReceiverPhone()+ " "+orderShippingDO.getReceiverProvince()+" "+orderShippingDO.getReceiverCity()+" "+orderShippingDO.getReceiverCounty()+" "+orderShippingDO.getReceiverAddress()+ "\n" +
				"★售后原因："+backReason+"\n" +
				"★售后类型："+ OrderStatus.getStatus(orderOrderDO.getStatus()) +"\n" +
				"★我的处理方案：手动填写\n" +
				"★退款金额：手动填写";
		model.addAttribute("content", content);
		return "wx/orderOrder/orderBackFormat";
	}
}
